package com.zHan.system.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zHan.common.result.Result;
import com.zHan.common.utils.MD5Util;
import com.zHan.model.dto.SysUserQueryDto;
import com.zHan.model.system.SysUser;
import com.zHan.system.anno.Log;
import com.zHan.system.exception.MyException;
import com.zHan.system.menu.BusinessType;
import com.zHan.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zHan
 * @since 2023-06-22
 */
@RestController
@Slf4j
@RequestMapping("/admin/system/sysUser")
@Api(tags = "用户管理接口")
public class SysUserController {
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 新增用户
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation(value = "新增用户")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    public Result save(@RequestBody SysUser sysUser) {
        String phonePattern = "^1[3|4|5|7|8][0-9]{9}$";
        // 手机号判断
        if (!Pattern.matches(phonePattern, sysUser.getPhone())) {
            throw new MyException(201, "请输入正确的手机号");
        }
        // md5加密
        sysUser.setPassword(MD5Util.encrypt(sysUser.getPassword()));

        boolean isSuccess = sysUserService.save(sysUser);
        if (isSuccess) {
            return Result.ok(sysUser);
        }
        return Result.fail();
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "删除用户")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    public Result remove(@PathVariable Long id) {
        boolean isSuccess = sysUserService.removeUserById(id);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 批量删除用户
     *
     * @param idList
     * @return
     */
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "批量删除用户")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean isSuccess = sysUserService.removeUserByIds(idList);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 修改用户
     *
     * @param sysUser
     * @return
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "修改用户")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result update(@RequestBody SysUser sysUser) {
        boolean isSuccess = sysUserService.updateById(sysUser);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据id查询单个用户
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询单个用户")
    public Result findUser(@PathVariable Long id) {
        SysUser user = sysUserService.getById(id);
        if (user != null) {
            return Result.ok(user);
        }
        return Result.fail();
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param pageSize
     * @param userQueryVo
     * @return
     */
    @GetMapping("/{page}/{pageSize}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "用户分页条件查询")
    public Result findPageQueryUser(@ApiParam(name = "page", value = "当前页码", required = true)
                                    @PathVariable Long page,
                                    @ApiParam(name = "pageSize", value = "每页记录数", required = true)
                                    @PathVariable Long pageSize,
                                    @ApiParam(name = "roleQueryVo", value = "查询对象")
                                    SysUserQueryDto userQueryVo) {
        Page<SysUser> pageInfo = sysUserService.selectPageQuery(page, pageSize, userQueryVo);
        //返回
        return Result.ok(pageInfo);
    }

    /**
     * 更改用户状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/updateStatus/{id}/{status}")
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "更改用户状态")
    @Log(title = "用户管理", businessType = BusinessType.STATUS)
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean isSuccess = sysUserService.updateStatus(id, status);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }
}

